﻿using GodSystemBLL;
using GodSystemCommon;
using GodSystemModel;
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormRolePower : Form
    {
        public static RolesModel rolesModel;
        public static PowersModel powersModel;
        public static PowerGroupModel powerGroupModel;
        public FormRolePower()
        {
            InitializeComponent();
            tabPage_RoleListEvent(this, null);
        }

        private void tabControl_RolePowerEvent(object sender, TabControlEventArgs args)
        {
            if (args.TabPage == tabPage_RoleList)
            {
                tabPage_RoleListEvent(this, args);
            }
            else if (args.TabPage == tabPage_PowerList)
            {
                tabPage_PowerListEvent(this, args);
            }
        }

        /// <summary>
        /// 展示角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void tabPage_RoleListEvent(object sender, TabControlEventArgs args)
        {
            RolesBLL rolesBLL = new RolesBLL();
            List<RolesModel> roleList = rolesBLL.GetRolesList();
            if (Utility.CheckUserPower("CheckRoles"))
            {
                Dymical_Add_Button_ForRole(roleList);
            }
            
        }

        /// <summary>
        /// 展示权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void tabPage_PowerListEvent(object sender, TabControlEventArgs args)
        {
            if (Utility.CheckUserPower("CheckPower"))
            {
                PowersBLL powersBLL = new PowersBLL();
                List<PowersModel> powersList = powersBLL.GetPowersList();
                List<PowerGroupModel> powerGroup = powersBLL.GetPowerGroup();
                Dymical_Display_Power(powersList, powerGroup);
            }

            int x = 60;
            int y = 26;
            int width = 100;
            int sep = 30;

            if (!Utility.CheckUserPower("AddPowerGroup"))
            {
                button_PowerGroupAdd.Visible = false;
            }
            else
            {
                button_PowerGroupAdd.Visible = true;
                button_PowerGroupAdd.Location = new Point(x, y);
                x += width + sep;
            }


            if (!Utility.CheckUserPower("AddPower"))
            {
                button_PowerAdd.Visible = false;
            }
            else
            {
                button_PowerAdd.Visible = true;
                button_PowerAdd.Location = new Point(x, y);
                x += width + sep;
            }

            
            if (!Utility.CheckUserPower("UpdatePowerGroup"))
            {
                button_ModifyPower.Visible = false;
            }
            else
            {
                button_ModifyPower.Visible = true;
                button_ModifyPower.Location = new Point(x, y);
            }
        }

        /// <summary>
        /// 动态展示权限列表
        /// </summary>
        /// <param name="powersList"></param>
        public void Dymical_Display_Power(List<PowersModel> powersList, List<PowerGroupModel> powerGroup)
        {
            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("ID", Type.GetType("System.Int32"));
            DataColumn dc2 = new DataColumn("权限名称", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("所属分组", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("节点名称", Type.GetType("System.String"));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            if (powersList.Count > 0)
            {
                foreach (PowerGroupModel pg in powerGroup)
                {
                    ht[pg.Id] = pg.GroupName;
                }
            }
  
            for(int i = 0; i < powersList.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = powersList[i].Id;
                dr["权限名称"] = powersList[i].Remark;
                // dr["所属分组"] = groupNameList[powersList[i].PowerGroupId];
                if (ht.ContainsKey(powersList[i].PowerGroupId))
                {
                    dr["所属分组"] = ht[powersList[i].PowerGroupId];
                }
                else
                {
                    dr["所属分组"] = "未分组";
                }
                dr["节点名称"] = powersList[i].Node;
                dt.Rows.Add(dr);
            }
            dataGridView_PowerList.DataSource = dt;
        }

        /// <summary>
        /// 展示角色列表
        /// </summary>
        /// <param name="roleList"></param>
        private void Dymical_Add_Button_ForRole(List<RolesModel> roleList)
        {
            int locationXStart = 20;
            int locationY = 20;
            int sizeX = 100;
            int sizeY = 30;
            int rowNum = 6;
            int j = 0;
            for(int i = 0; i < roleList.Count; i++)
            {
                // 换一行
                if (i > 0 && i % rowNum == 0)
                {
                    locationY = locationY + 20 + 20;
                    j = 0;
                }
                int locationX = locationXStart + j * sizeX + j * locationXStart;
                j++;
                Button bt = new Button();
                bt.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                bt.AutoSize = false;
                bt.Dock = DockStyle.None;
                bt.Location = new Point(locationX, locationY);
                bt.Size = new Size(sizeX, sizeY);
                bt.Text = roleList[i].RoleName;
                bt.Tag = roleList[i];
                panel_role.Controls.Add(bt);
                if(Utility.CheckUserPower("RoleUpdate") || Utility.CheckUserPower("UpdateRolePower"))
                {
                    bt.Click += ModifyRoles_Dymical_Click;
                }
                

            }

            if (Utility.CheckUserPower("RoleNewAdd"))
            {
                locationY = locationY + 20 + 20;

                Button btAdd = new Button();
                btAdd.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                btAdd.AutoSize = false;
                btAdd.Dock = DockStyle.None;
                btAdd.Location = new Point(locationXStart, locationY);
                btAdd.Size = new Size(sizeX, sizeY);
                btAdd.Text = "新增角色";
                btAdd.BackColor = Color.Blue;
                btAdd.ForeColor = Color.White;
                panel_role.Controls.Add(btAdd);
                btAdd.Click += AddRoles_Click;
            }


        }
        /// <summary>
        /// 动态角色操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ModifyRoles_Dymical_Click(object sender, EventArgs e)
        {
            string originalRoleName = ((Button)sender).Text.ToString();
            RolesModel role = (RolesModel)((Button)sender).Tag;
            rolesModel = new RolesModel();
            rolesModel.RoleName = originalRoleName;
            rolesModel.Id = role.Id;
            // 实例化在后
            FormRoleModify roleModify = FormRoleModify.CreateInstance();
            roleModify.Activate();
            roleModify.Show();
        }


        /// <summary>
        /// 增加角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddRoles_Click(object sender, EventArgs e)
        {
            FormAddRoles formAddRoles = new FormAddRoles();
            formAddRoles.Show();
        }

        private void button_PowerGroupAdd_Click(object sender, EventArgs e)
        {
            FormPowerGroupAdd formPowerGroupAdd = new FormPowerGroupAdd();
            formPowerGroupAdd.Show();
        }

        private void button_PowerAdd_Click(object sender, EventArgs e)
        {
            FormAddPower formAddPower = new FormAddPower();
            formAddPower.Show();
        }

        private void button_ModifyPower_Click(object sender, EventArgs e)
        {
            // 获取表格当前选择的行,即使点击是单元格，也可以获取整个行
            int index = dataGridView_PowerList.CurrentRow.Index;
            if (index < 0)
            {
                MessageBox.Show("请选择要修改的权限", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            PowersBLL powersBLL = new PowersBLL();
            List<PowerGroupModel> powerGroup = powersBLL.GetPowerGroup();
            Dictionary<string, int> groupNameId = new Dictionary<string, int>();
            groupNameId.Add("未分组", 0);
            for (int i = 0; i < powerGroup.Count; i++)
            {
                groupNameId.Add(powerGroup[i].GroupName, powerGroup[i].Id);
            }
            

            string powerIdStr = dataGridView_PowerList.Rows[index].Cells[0].Value.ToString();
            string remark = dataGridView_PowerList.Rows[index].Cells[1].Value.ToString();
            string groupIdStr = dataGridView_PowerList.Rows[index].Cells[2].Value.ToString();
            int groupId = groupNameId[groupIdStr];
            int id = Convert.ToInt32(powerIdStr);
            
            PowersModel power = new PowersModel();
            power.Remark = remark;
            power.Id = id;
            power.PowerGroupId = groupId;
            powersModel = power;
            FormPowerModify powerModify = FormPowerModify.CreateInstance();
            powerModify.Activate();
            powerModify.Show();
            
        }
    }
}
